package com.xsm.ppsecret.ui.ease;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseBaseActivity;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.jaeger.library.StatusBarUtil;
import com.xsm.ppsecret.R;
import com.xsm.ppsecret.util.UiSetting;

public class ChatActivity extends EaseBaseActivity {

    private EaseChatFragment chatFragment;
    private ChatActivity activityInstance;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.appColor), 0);
        UiSetting.setStatusBarDarkModel(this);
        activityInstance = this;

        userId = getIntent().getStringExtra("userId");
        if (userId != null && !userId.isEmpty()) {
            Bundle args = new Bundle();
            args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
            args.putString(EaseConstant.EXTRA_USER_ID, userId);
            chatFragment = new EaseChatFragment();
            //set arguments
            chatFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(R.id.activity_chat, chatFragment).commit();
        } else {
            Toast.makeText(this, "该用户已注销", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // enter to chat activity when click notification bar, here make sure only one chat activiy
        String username = intent.getStringExtra("userId");
        if (userId.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }

    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }

    public String getToChatUsername(){
        return userId;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
    }
}
